package com.culturesalon.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.culturesalon.dao.GoodsDAO;
import com.culturesalon.dao.GoodsDAOImpl;
import com.culturesalon.dao.NoticeCategoryDAO;
import com.culturesalon.dao.NoticeCategoryDAOImpl;
import com.culturesalon.dao.NoticeDAO;
import com.culturesalon.dao.NoticeDAOImpl;
import com.culturesalon.dto.Goods;
import com.culturesalon.dto.Notice;
import com.culturesalon.dto.NoticeCategory;
import com.culturesalon.util.StringUtil;

/**
 * Servlet implementation class NoticeUpdate
 */
@WebServlet("/noticeupdate.do")
public class NoticeUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NoticeUpdate() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
		String action = request.getParameter("actionx");
		NoticeCategoryDAO noticeCategoryDAO=new NoticeCategoryDAOImpl();
		List<NoticeCategory> noticeCategories=noticeCategoryDAO.findAll();
		if (StringUtil.isEmpty(id)) {
			request.getRequestDispatcher("noticelist.do").forward(request,
					response);
		} else {
			if (action != null && action.equals("update")) {
				
				NoticeDAO noticedao = new NoticeDAOImpl();
				Notice notice = noticedao.get(Integer.parseInt(id));
				
				
				request.setAttribute("noticeCategories", noticeCategories);
				request.setAttribute("notice", notice);
				request.setAttribute("mainpage", "noticeupdate.jsp");
				request.getRequestDispatcher("admin/adminindex.jsp").forward(
						request, response);

			} else {
				request.setCharacterEncoding("UTF-8");

				String title = request.getParameter("title");
				String date = request.getParameter("date");
				String content= request.getParameter("content");
				String author = request.getParameter("author");
				String recommend = request.getParameter("recommend");
				String categoryId = request.getParameter("categoryId");
				if (!StringUtil.isEmpty(title)) {
					// &&!StringUtil.isEmpty(categoryId)
					Notice notice = new Notice();
				
					notice.setTitle(title);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

					try {
					notice.setDate(sdf.parse(date));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					notice.setContent(content);
					notice.setAuthor(author);
					notice.setRecommend(Boolean.parseBoolean(recommend));
					notice.setCategoryId(Integer.parseInt(categoryId));
					
					NoticeDAO noticeDAO = new NoticeDAOImpl();
					notice.setId(Integer.parseInt(id));
					noticeDAO.update(notice);
					//request.setAttribute("mainpage", "/CultureSalon/goodsupdate.do");
					request.getRequestDispatcher("noticelist.do")
							.forward(request, response);
				}
			}
		}
	}


	
	

}
